YZYZ.Admin 					= {};
YZYZ.Admin.News 			= {};
YZYZ.Admin.News.gridPanel 	= null;
YZYZ.Admin.News.columnModel = null;
YZYZ.Admin.News.store 		= null;
YZYZ.Admin.News.newWin 		= null;
YZYZ.Admin.News.newForm 	= null;
YZYZ.Admin.News.addData 	= [[0,0],[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9]];
YZYZ.Admin.News.addCombox 	= null;
YZYZ.Admin.News.editWin 	= null;
YZYZ.Admin.News.editForm 	= null;
YZYZ.Admin.News.editCombox  = null;
YZYZ.Admin.News.uploadWin 	= null;
YZYZ.Admin.News.showUploadWin = function(id){
	if(!YZYZ.Admin.News.uploadWin){
		YZYZ.Admin.News.uploadWin = new Ext.Window({
			title:'资讯图片',
			width:500,
			height:400,
			closable:true,
			closeAction:'hide',
			contentEl:'ifrUpload',
			layout:'fit',
			buttons:[{
				text:'关闭',
				width:60,
				handler:function(){
					YZYZ.Admin.News.uploadWin.hide();
				}
			}]
		});
	}
	YZYZ.Admin.News.uploadWin.show();
	Ext.get("ifrUpload").dom.src = Ext.get("uploadUrl").dom.value + id + "&temp=" + Math.random();
};
YZYZ.Admin.News.deleteConfirm = function(id){
	Ext.MessageBox.show({
		title:'确认',
		msg:'您确定要删除这条资讯吗？',
		buttons:Ext.MessageBox.OKCANCEL,
		fn:function(btn){YZYZ.Admin.News.handle(btn,id);}
	})
};
YZYZ.Admin.News.handle = function(btn, id){
	if(btn != 'ok'){
		return;
	}
	Ext.Ajax.request({
		url		: Ext.get("deleteUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			newsId 	:  id
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("删除成功");
				YZYZ.Admin.News.gridPanel.getStore().reload();	
			}
			else {
				YZYZ.Curtain.alert(result.message);
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序错误或网络故障");
		}
	});
};
YZYZ.Admin.News.submitEditNews = function(){
	if(YZYZ.Admin.News.editForm.getForm().isValid()){
		YZYZ.Admin.News.editForm.getForm().submit({
			url: Ext.get("updateUrl").dom.value,
		 	waitMsg : '正在保存数据，请耐心等待...',
			success:function(form, action){
				YZYZ.Curtain.msg(action.result.msg);
				YZYZ.Admin.News.gridPanel.getStore().reload();
			},
			failure:function(form, action){
				YZYZ.Curtain.alert(action.result.msg);
			}
		});
	}
};
YZYZ.Admin.News.showEditWin = function(){
	if(!YZYZ.Admin.News.editWin){
		YZYZ.Admin.News.editForm = new Ext.form.FormPanel({
			region:'center',
			labelWidth:100,
			labelAlign:'right',
			autoHeight:true,
			id:'editForm',
			bodyStyle:'padding-top:20px',
			width:700,
			items:[{
				xtype:'textfield',
				name:'id',
				hidden:true
			},{
				xtype:'textfield',
				fieldLabel:'资讯标题',
				width:500,
				maxLength:100,
				id:'editTitle',
				name:'name',
				validator:function(value){
					if(value.length == 0 || value.length > 100){
						YZYZ.Curtain.alert("标题不能为空且长度不能超过100");
						return "";
					}
					return true;
				}
			},
			YZYZ.Admin.News.editCombox,
			{
			    xtype: 'htmleditor',
		        id:'editNewsContents',
		        enableColors: true,
		        name:'contents',
		        enableAlignments: true,
		        width:600,
		        height:350,
		        validator:function(value){
		        	if(value.length == 0){
		        		YZYZ.Curtain.alert("请填写新闻内容");
		        		return "";
		        	}
		        	if(value.length > 10000){
		        	    YZYZ.Curtain.alert("新闻内容长度不能超过10,000");
		        		return "";
		        	}
		        	return true;
		        }
			}]
		});
		YZYZ.Admin.News.editWin = new Ext.Window({
			title:'编辑旅游资讯',
			width:750,
			height:500,
			layout:'border',
			closable:true,
			closeAction:'hide',
			resizable:false,
			buttons:[{
				text:'提交',
				width:60,
				handler:YZYZ.Admin.News.submitEditNews
			},{
				text:'取消',
				width:60,
				handler:function(){
					YZYZ.Admin.News.editWin.hide();
				}
			}],
			items:[
				YZYZ.Admin.News.editForm
			]
		});
	}
	YZYZ.Admin.News.editWin.show();
	YZYZ.Admin.News.editForm.getForm().loadRecord(YZYZ.Admin.News.gridPanel.getSelectionModel().getSelected());
};
/**
 * 提交新添加的旅游资讯
 */
YZYZ.Admin.News.submitNewNews = function(){
	if(YZYZ.Admin.News.newForm.getForm().isValid()){
		YZYZ.Admin.News.newForm.getForm().submit({
			url: Ext.get("addUrl").dom.value,
		 	waitMsg : '正在保存数据，请耐心等待...',
			success:function(form, action){
				YZYZ.Curtain.msg(action.result.msg);
				YZYZ.Admin.News.gridPanel.getStore().load({params:{start:0,limit:30}});
			},
			failure:function(form, action){
				YZYZ.Curtain.alert(action.result.msg);
			}
		});
	}
};
YZYZ.Admin.News.addNews = function(){
	if(!YZYZ.Admin.News.newWin){
		YZYZ.Admin.News.newForm = new Ext.form.FormPanel({
			region:'center',
			labelWidth:100,
			labelAlign:'right',
			autoHeight:true,
			id:'newForm',
			bodyStyle:'padding-top:20px',
			width:700,
			items:[{
				xtype:'textfield',
				fieldLabel:'资讯标题',
				width:500,
				maxLength:100,
				id:'addTitle',
				name:'name',
				validator:function(value){
					if(value.length == 0 || value.length > 100){
						YZYZ.Curtain.alert("标题不能为空且长度不能超过100");
						return "";
					}
					return true;
				}
			},
			YZYZ.Admin.News.addCombox,
			{
			    xtype: 'htmleditor',
		        id:'addNewsContents',
		        enableColors: true,
		        name:'contents',
		        enableAlignments: true,
		        width:600,
		        height:350,
		        validator:function(value){
		        	if(value.length == 0){
		        		YZYZ.Curtain.alert("请填写新闻内容");
		        		return "";
		        	}
		        	if(value.length > 10000){
		        	    YZYZ.Curtain.alert("新闻内容长度不能超过10,000");
		        		return "";
		        	}
		        	return true;
		        }
			}]
		});
		YZYZ.Admin.News.newWin = new Ext.Window({
			title:'添加旅游资讯',
			width:750,
			height:500,
			layout:'border',
			closable:true,
			closeAction:'hide',
			resizable:false,
			buttons:[{
				text:'提交',
				width:60,
				handler:YZYZ.Admin.News.submitNewNews
			},{
				text:'取消',
				width:60,
				handler:function(){
					YZYZ.Admin.News.newWin.hide();
				}
			}],
			items:[
				YZYZ.Admin.News.newForm
			]
		});
	}
	YZYZ.Admin.News.newWin.show();
};
YZYZ.Admin.News.opRender = function(value, metaData, record, rowIndex, colIndex, store){
	var html = "<div class='btnCotainer'>";
	html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.News.showEditWin()' class='btnEdit'>编辑</button></div>";
	html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.News.showUploadWin("+record.get('id')+")' class='btnEdit'>图片</button></div>";
	html = html + "<div class='divBtn'><button type='button' class='btnEdit' onclick='YZYZ.Admin.News.deleteConfirm("+record.get('id')+")'>删除</button></div>";
	html = html + "</div>"
	return html;
};
YZYZ.Admin.News.init = function(){
	YZYZ.Admin.News.addDataSotre = new Ext.data.SimpleStore({  
	  fields:['id','name'],  
	  data  :YZYZ.Admin.News.addData 
 	 });
 	YZYZ.Admin.News.editCombox = new Ext.form.ComboBox({  
	         id: 'cxEditLevel', 
	         allowBlank: false,  
	         fieldLabel: '优先级',  
	         name: 'level',  
	         displayField: 'name',  
	         valueField: 'id',  
	         typeAhead: true,  
	         triggerAction: 'all',  
	         store: YZYZ.Admin.News.addDataSotre,  
	         mode: 'local',
	         width:300 ,
	         editable:false,
	         validator:function(value){
	         	if(value.length == 0){
	         		YZYZ.Curtain.alert("请选择优先级");
	         		return "";
	         	}
	         	return true;
	         }
	 });
	YZYZ.Admin.News.addCombox = new Ext.form.ComboBox({  
	         id: 'cxAddLevel', 
	         allowBlank: false,  
	         fieldLabel: '优先级',  
	         name: 'level',  
	         displayField: 'name',  
	         valueField: 'id',  
	         typeAhead: true,  
	         triggerAction: 'all',  
	         store: YZYZ.Admin.News.addDataSotre,  
	         mode: 'local',
	         width:300 ,
	         editable:false,
	         validator:function(value){
	         	if(value.length == 0){
	         		YZYZ.Curtain.alert("请选择优先级");
	         		return "";
	         	}
	         	return true;
	         }
	 });
	YZYZ.Admin.News.columnModel = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
            {header: '标题', width: 300, sortable: false, hideable:false, dataIndex: 'name'},
            {header: '日期', width: 100, sortable: false, hideable:false, dataIndex: 'date'},
            {header: '显示级别', width: 100, sortable: false, hideable:false, dataIndex: 'level'},
            {header: '操作',id:'operations', width: 250, sortable: false, hideable:false, dataIndex: 'id', renderer:YZYZ.Admin.News.opRender}
    	]);
	
	YZYZ.Admin.News.store = new Ext.data.Store({ 
		proxy: new Ext.data.HttpProxy ({
	        url: document.getElementById("newsUrl").value
	    }),
	    reader: new Ext.data.JsonReader({
	    	root			: "data",
	    	totalProperty	: "sum"
		}, [
	        {name:'id'},
	        {name:'name'},
	        {name:'contents'},
	        {name:'date'},
	        {name:'level', type:'int'}
	    ]),
	    remoteSort: false
	});
	YZYZ.Admin.News.gridPanel = new Ext.grid.GridPanel({
			id 			: 'newsGrid',
			region		: 'center',
			height		: Ext.getBody().getHeight()-100,
			width		: Ext.getBody().getWidth(),
			store 		: YZYZ.Admin.News.store,
			sm			: new Ext.grid.RowSelectionModel(),
			cm 			: YZYZ.Admin.News.columnModel,
			viewConfig	: { forceFit:true },
			autoExpandColumn : 'operations',
			loadMask	: { msg:"数据加载中....." },
			bbar		: new Ext.PagingToolbar({   
				pageSize	: 15,   
				store		: YZYZ.Admin.News.store,   
				displayInfo	: true,   
				displayMsg	: "第{0}条到{1}条  共{2}条",   
				emptyMsg	: "没有旅游资讯",
				items		:[{text:'添加资讯',handler:YZYZ.Admin.News.addNews}]
			})   
	});
	new Ext.Viewport({
		layout: 'border',
		items:[{
			region			: 'center',
			cls				: 'divGrid',
			items 			: [YZYZ.Admin.News.gridPanel],
			autoScroll		: false,
			layout			: 'fit' 
		}]
	});
	YZYZ.Admin.News.gridPanel.getStore().load({params:{start:0,limit:15}});
};
Ext.onReady(YZYZ.Admin.News.init, document);